﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Fares
{
    public partial class PlatiRezumatFrm : FrmRezumat
    {
        private BindingSource bsDoc = null;
        private BindingSource bsDocDetaliu = null;
        public Int32 IDDocPlataSel = -1;        

        public PlatiRezumatFrm(ModuleClass mc,BindingSource ds,BindingSource dsd,Int32 docPl):base(mc)
        {
            IDDocPlataSel = docPl;
            bsDoc = ds;
            bsDocDetaliu = dsd;
            ReadOnly = true;
            InitializeComponent();
            workConnString = mc.workConnectionString;
            ReadOnly = true;
            EnableMainBrowseButton = false;
            documentePlataTableAdapter.SetTableName(mc.AnArhiva);
            fares_DocumentePlatiteTableAdapter.SetTableName(mc.AnArhiva);


            
            this.dataGridViewTextBoxColumn4.DisplayMember = "Denumire";
            this.dataGridViewTextBoxColumn4.ValueMember = "IDTipPlata";
        }

        private void PlatiRezumatFrm_Load(object sender, EventArgs e)
        {
            AddBindingSource(documentePlataBindingSource);
            AddBindingSource(fKDocumentePlataDetaliuDocumentePlataBindingSource);

            SqlConnection con = DeschideConexiune(workConnString);
            this.fares_ParteneriCUITableAdapter.Connection = con;
            this.fares_ParteneriCUITableAdapter.Fill(this.esrpDataSetPlati.fares_ParteneriCUI);
            this.fares_DocumentePlatiteTableAdapter.Connection = con;
            this.fares_DocumentePlatiteTableAdapter.Fill(this.esrpDataSetPlati.fares_DocumentePlatite);
            documentePlataDetaliuTableAdapter.Connection = con;
            documentePlataDetaliuTableAdapter.Fill(esrpDataSetPlati.DocumentePlataDetaliu);

           
            this.tipDocumentPlataTableAdapter.Connection = con;
            this.tipDocumentPlataTableAdapter.Fill(this.esrpDataSetPlati.TipDocumentPlata);
            documentePlataTableAdapter.Connection = con;
            documentePlataTableAdapter.Fill(esrpDataSetPlati.DocumentePlata, ModuleInfo.CodUnitate);
            InchideConexiune(con);

            //documentePlataDGV.DataSource = bsDoc;
            //documentePlataDetaliuDGV.DataSource = bsDocDetaliu;
            documentePlataBindingSource.Sort = "Data DESC";
            if (IDDocPlataSel != -1)
            {
                int pos = documentePlataBindingSource.Find("IDDocumentPlata", IDDocPlataSel);
                if (pos != -1)
                    documentePlataBindingSource.Position = pos;
            }

        }

        private void PlatiRezumatFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (selectedRow != null)
            {
                DataSets.ESRPDataSetPlati.DocumentePlataRow dr = currentRow.Row as DataSets.ESRPDataSetPlati.DocumentePlataRow;
                if(bsDoc!=null)
                    bsDoc.Position = bsDoc.Find("IDDocumentPlata", dr.IDDocumentPlata);
            }
        }
    }

    public class fares_DocumentePlatiteTableAdapterEx : DataSets.ESRPDataSetPlatiTableAdapters.fares_DocumentePlatiteTableAdapter
    {
        public fares_DocumentePlatiteTableAdapterEx()
            : base()
        {

        }
        public void SetTableName(String an)
        {
            foreach (SqlCommand cmd in this.CommandCollection)
            {
                if (cmd.CommandText.Contains("dbo.fares_DocumentePlatite"))
                    cmd.CommandText = cmd.CommandText.Replace("FROM dbo.fares_DocumentePlatite", "FROM [fares_DocumentePlatite" + an + "]");
                else
                    cmd.CommandText = cmd.CommandText.Replace("FROM fares_DocumentePlatite", "FROM [fares_DocumentePlatite" + an + "]");
            }
        }
    }
}
